package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult selectAllDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<Dish> list = dishMapper.selectAllDish(dishPageQueryDTO);
        Page<Dish> page = (Page<Dish>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        Long id = BaseContext.getCurrentId();
        dish.setUpdateUser(id);
        dish.setCreateUser(id);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insertDish(dish);
        //添加菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null||flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insert(flavors);
        }

    }

    @Override
    @Transactional
    public void deltesDish(List<Integer> ids) {
        //判断是否还在起售中
        for (Integer id : ids) {
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断是否还有关联的商品

        //删除菜品及菜品的口味
        //删除商品
        dishMapper.deleteById(ids);
        //删除商品口味
        dishFlavorMapper.deleteById(ids);
    }

    @Override
    public DishVO getById(Long id) {
        //根据id查询信息
        DishVO dishVo = dishMapper.getById(id);
        //根据id查询口味
        List<DishFlavor> list = dishFlavorMapper.getById(id);
        dishVo.setFlavors(list);
        return dishVo;
    }

    @Override
    @Transactional
    public void updateById(DishVO dishVO) {
        //修改基本信息
        dishVO.setUpdateTime(LocalDateTime.now());
        dishMapper.updateById(dishVO);
        //修改口味
        //先删除所有的口味，然后重新添加
        Long id = dishVO.getId();
        dishFlavorMapper.delete(id);
        List<DishFlavor> flavors = dishVO.getFlavors();
        if(flavors!=null||flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public void updateStatusById(Integer status, Long id) {
        //判断套餐中有没有售卖，如果有售卖，那就不能禁售
        if(status == StatusConstant.DISABLE){
            List<Long> ids = dishMapper.selectDishBySetmealId(id);
            for (Long d : ids) {
                List<SetmealVO> list = setmealMapper.selectSetmealById(d);
                for (SetmealVO s : list) {
                    if (s.getStatus()==StatusConstant.ENABLE){
                        System.out.println("该菜品关联了正在启售中的套餐，不能禁用");
                        throw new DeletionNotAllowedException("该菜品关联了正在启售中的套餐，不能禁用");
                    }
                }
            }
        }
        dishMapper.updateStatusById(status, id);
    }

    @Override
    public List<DishVO> selectDishByCategoryId(Long categoryId) {
        return dishMapper.selectDishByCategoryId(categoryId);
    }

}